package com.ruoyi.dm.component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.ruoyi.common.constant.CollectFileTypeConstants;
import com.ruoyi.common.utils.biz.BizModule;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.dm.domain.po.GroundCollectRecordPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.input.ReversedLinesFileReader;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;

/**
 * 地基 无线电探空大气监测数据
 */
@Component
@Slf4j
public class RadioDataStrategy implements DataStrategy<GroundCollectRecordPO> {

    @Override
    public GroundCollectRecordPO makeupPo(GroundCollectRecordPO po) {

        ///dgdq/output/collect/origin/wind/sounding
        String fullPath = PathUtil.outputModuleDir(BizModule.COLLECT)+po.getFilePath();
        File path = new File(fullPath, po.getFileName());
        ReversedLinesFileReader reader = null;
        try {
            reader = ReversedLinesFileReader.builder().setFile(path).get();
            String line = "";
            while (null != (line=reader.readLine())) {
                // 截取最新数据时间
                //  #AYM00089601 1958 02 28 00 9999    9 ncdc6310          -662700  1007300
                if (line.startsWith("#")) {
                    String[] split = line.split("\\s|#");
                    LocalDate of = LocalDate.of(Integer.valueOf(split[2]), Integer.valueOf(split[3]), Integer.valueOf(split[4]));
                    po.setFileDate(of);
                    po.setStation(split[1]);
                    break;
                }
            }
        } catch (IOException e) {
            log.error("解析时间异常",e);
        }
        return po;
    }


    @Override
    public String pythonCmd(String satellite, String dataType, String filePath, String method, Integer year) {
        String fileType = CollectFileTypeConstants.TYPE_RADIO;
        //
        String outPath = PathUtil.outputModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result") ;
        FileUtil.mkdir(outPath);
        String logPath = PathUtil.tmpModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result") ;
        FileUtil.mkdir(logPath);
        String script = PathUtil.codeModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("radioScript");
        String[] cmdLines = new String[12];
        //原始文件 放到 /dgdq/output/collect/ground/RADIO
        cmdLines[0] = "python";
        cmdLines[1] = script;
        cmdLines[2] = "--name_list";//站点数据
        cmdLines[3] = "/dgdq/code/dgdq-algorithm/collect/igra2-station-list.txt";
        cmdLines[4] = "--outfile";//产品路径
        cmdLines[5] = outPath;
        cmdLines[6] = "--outlog";//日志路径
        cmdLines[7] = outPath;
        cmdLines[8] = "--times";//解析时间 2021112500
        cmdLines[9] = year+"";
        cmdLines[10] = "--inter_type";//插值方式
        cmdLines[11] = method;
        String commond = ArrayUtil.join(cmdLines, " ");
        log.info("拼接脚本:{}", commond);
        return commond;
    }
}
